package com.dragon.modules.yuegou.model.coupon;


import com.dragon.modules.yuegou.utils.BitCodeUtils;

/**
 * 支持的币种（把数字看为二进制序列，从右往左数，第0位表示：悦币、第1位表示：发放的悦卡、第2位表示：购买的悦卡、第3位表示：积分、第4位表示：第三方钱包）
 * 
 * @author liuwu
 *
 */
public class CurrencyCode {

	private static final int YUE_COIN_IDX = 0;
	private static final int YUE_CARD_GRANTED_IDX = 1;
	private static final int YUE_CARD_BOUGHT_IDX = 2;
	private static final int POINTS_IDX = 3;
	private static final int THIRD_WALLET_IDX = 4;

	private int code;

	public CurrencyCode(int code) {
		super();
		this.code = code;
	}

	public int getCode() {
		return code;
	}

	public boolean isYueCoin() {
		return BitCodeUtils.get(code, YUE_COIN_IDX);
	}

	public void setYueCoin(boolean bit) {
		code = BitCodeUtils.set(code, YUE_COIN_IDX, bit);
	}

	public boolean isYueCardGranted() {
		return BitCodeUtils.get(code, YUE_CARD_GRANTED_IDX);
	}

	public void setYueCardGranted(boolean bit) {
		code = BitCodeUtils.set(code, YUE_CARD_GRANTED_IDX, bit);
	}

	public boolean isYueCardBought() {
		return BitCodeUtils.get(code, YUE_CARD_BOUGHT_IDX);
	}

	public void setYueCardBought(boolean bit) {
		code = BitCodeUtils.set(code, YUE_CARD_BOUGHT_IDX, bit);
	}

	public boolean isPoints() {
		return BitCodeUtils.get(code, POINTS_IDX);
	}

	public void setPoints(boolean bit) {
		code = BitCodeUtils.set(code, POINTS_IDX, bit);
	}

	public boolean isThirdWallet() {
		return BitCodeUtils.get(code, THIRD_WALLET_IDX);
	}

	public void setThirdWallet(boolean bit) {
		code = BitCodeUtils.set(code, THIRD_WALLET_IDX, bit);
	}
	
	public void and(CurrencyCode other) {
		this.code &= other.getCode();
	}
	
	public void or(CurrencyCode other) {
		this.code |= other.getCode();
	}
	
	public boolean contains(CurrencyCode that) {
		return (this.code | that.code) == this.code;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("CurrencyCode [code=");
		builder.append(Integer.toBinaryString(code));
		builder.append(", isYueCoin()=");
		builder.append(isYueCoin());
		builder.append(", isYueCardGranted()=");
		builder.append(isYueCardGranted());
		builder.append(", isYueCardBought()=");
		builder.append(isYueCardBought());
		builder.append(", isPoints()=");
		builder.append(isPoints());
		builder.append(", isThirdWallet()=");
		builder.append(isThirdWallet());
		builder.append("]");
		return builder.toString();
	}

}
